﻿package com.sharemarking.application.common.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sharemarking.application.common.entity.Comment;
import com.sharemarking.application.common.entity.Share;
import com.sharemarking.application.common.repository.CommentRepo;
import com.sharemarking.application.common.repository.EmployeeRepo;
import com.sharemarking.application.common.repository.ShareRepo;



@Controller
@RequestMapping("function")
public class PhoneCommentController {
	
	@Autowired
	private CommentRepo co;
	@Autowired
	private ShareRepo so;
	@Autowired
	private EmployeeRepo em;
	
	//列出所有
	@RequestMapping(value = "findcomment")
	@ResponseBody
	public List<Share> findcomment() {
		List<Share> list=null;
		Sort sort = new Sort(Direction.DESC, "updateDate");
	    list= (List<Share>) so.findAll(sort);
		
		return list;
	}
	//未阅读的条数
	@RequestMapping(value = "count")
	@ResponseBody
	public Integer listcount(Integer from_uid,Integer to_uid) {
		Integer count=co.findBycountState(from_uid,to_uid);
		return count;
	}
	//列出状态为没有阅读的(并设置为已阅)
	@RequestMapping(value = "listcomment")
	@ResponseBody
	public List<Comment> listcomment(Integer from_uid,Integer to_uid){
		List<Comment> list=co.findBycState(from_uid, to_uid);
		for(int i=0;i<list.size();i++){
			Comment comment=new Comment();
			comment=list.get(i);
			comment.setState("1");
			co.save(comment);
		}
		return list;
	}
	
	//第一种为创建分享评论
	@RequestMapping(value = "insertscomment")
	@ResponseBody
	public void insertscomment(Comment comment,Integer from_id) {
		System.out.println("from_id:"+from_id);
		comment.setFromemployee(em.findOne(from_id));
		co.save(comment);
	}
		
	//第二种为回复评论
	@RequestMapping(value = "insertcomment")
	@ResponseBody
	public void insertcomment(Comment comment,Integer from_id,Integer to_id) {
		if(to_id==null){
			comment.setFromemployee(em.findOne(from_id));
			comment.setState("0");
		}else{
			comment.setFromemployee(em.findOne(from_id));
			comment.setToemployee(em.findOne(to_id));
			comment.setState("0");
		}
		//comment.setStarttime(new Date());
		co.save(comment);
	}
	
}
